// 这是日志输出配置分割插件
const { createLogger, format, transports } = require('winston')
require('winston-daily-rotate-file')

const customFormat = format.combine(
	format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
	format.align(),
	format.printf((i) => `${i.level}-${[i.timestamp]}: ${i.message.trimLeft()}`)
)
const defaultOptions = {
	format: customFormat,
	datePattern: 'YYYY-MM-DD',
	maxSize: '50m', // 最大日志文件大小,然后存另一个文件
	maxFiles: '7d', // 7天切换文件
	// format: format.simple(),
}

const logger = createLogger({
	format: customFormat,
	transports: [
		new transports.Console(),
		new transports.DailyRotateFile({
			filename: 'logs/out-%DATE%.log',
			level: 'info',
			...defaultOptions,
		}),
		new transports.DailyRotateFile({
			filename: 'logs/error-%DATE%.log',
			level: 'error',
			...defaultOptions,
		}),
	],
	exitOnError: false,
	exceptionHandlers: [
		new transports.DailyRotateFile({
			filename: 'logs/exceptions.log',
		}),
	],
})

// 将 console.log 绑定到 logger.info 方法
console.log = logger.info.bind(logger)

module.exports = logger
